package com.dog.modules.system.web;

import com.dog.framework.core.model.JwtUser;
import com.dog.framework.core.model.ResponseResult;
import com.dog.framework.log.annotation.OperateLog;
import com.dog.framework.log.enums.BusinessType;
import com.dog.framework.web.util.SecurityUserUtils;
import com.dog.modules.system.convert.SysMenuConvert;
import com.dog.modules.system.domain.SysMenu;
import com.dog.modules.system.enums.MenuTypeEnum;
import com.dog.modules.system.api.vo.SysMenuVO;
import com.dog.modules.system.service.SysMenuService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;
import java.util.Set;

/**
 * 菜单管理
 *
 * @author 阿沐 babamu@126.com
 * <a href="https://maku.net">MAKU</a>
 */
@RestController
@RequestMapping("/sys/menu")
@Tag(name = "菜单管理")
@AllArgsConstructor
public class SysMenuController {
    private final SysMenuService sysMenuService;

    @GetMapping("/nav")
    @Operation(summary = "菜单导航")
    public ResponseResult<List<SysMenuVO>> nav() {
        JwtUser user = SecurityUserUtils.getCurrentUser();
        List<SysMenuVO> list = sysMenuService.getUserMenuList(user, MenuTypeEnum.MENU.getValue());

        return ResponseResult.success(list);
    }

    @GetMapping("/authority")
    @Operation(summary = "用户权限标识")
    public ResponseResult<Set<String>> authority() {
        JwtUser user = SecurityUserUtils.getCurrentUser();
        Set<String> set = sysMenuService.getUserAuthority(user);

        return ResponseResult.success(set);
    }

    @GetMapping("/list")
    @Operation(summary = "菜单列表")
    @Parameter(name = "type", description = "菜单类型 0：菜单 1：按钮  2：接口  null：全部")
    public ResponseResult<List<SysMenuVO>> list(Integer type) {
        List<SysMenuVO> list = sysMenuService.getMenuList(type);

        return ResponseResult.success(list);
    }

    @GetMapping("{id}")
    @Operation(summary = "信息")
    public ResponseResult<SysMenuVO> get(@PathVariable("id") Long id) {
        SysMenu entity = sysMenuService.getById(id);
        SysMenuVO vo = SysMenuConvert.INSTANCE.convert(entity);

        // 获取上级菜单名称
        if (entity.getPid() != null) {
            SysMenu parentEntity = sysMenuService.getById(entity.getPid());
            vo.setParentName(parentEntity.getName());
        }

        return ResponseResult.success(vo);
    }

    @PostMapping
    @Operation(summary = "保存")
    @OperateLog(businessType = BusinessType.INSERT)
    public ResponseResult<String> save(@RequestBody @Valid SysMenuVO vo) {
        sysMenuService.save(vo);
        return ResponseResult.success();
    }

    @PutMapping
    @Operation(summary = "修改")
    @OperateLog(businessType = BusinessType.UPDATE)
    public ResponseResult<String> update(@RequestBody @Valid SysMenuVO vo) {
        sysMenuService.update(vo);
        return ResponseResult.success();
    }

    @DeleteMapping("{id}")
    @Operation(summary = "删除")
    @OperateLog(businessType = BusinessType.DELETE)
    public ResponseResult<String> delete(@PathVariable("id") Long id) {
        // 判断是否有子菜单或按钮
        Long count = sysMenuService.getSubMenuCount(id);
        if (count > 0) {
            return ResponseResult.fail("请先删除子菜单");
        }

        sysMenuService.delete(id);

        return ResponseResult.success();
    }
}